// locutor
// (c) 2008-2009, Normen Müller, normen.mueller@gmail.com  
// $Id: M.scala 2522 2009-10-26 10:00:51Z nmueller $
package locutor.model

import java.io.File
import java.text.MessageFormat.{format}

import scaup.Views._

//import locutor.Conf._
import locutor.model.parsing.dm.DM

case class MWarning(message: String) extends Throwable(message)
case class MError(message: String) extends Throwable(message)

/** 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.1 */
object M {
  
  def apply(model: File): M = new M(model)
}

/** A document model
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.1 */
class M private [model] (model: File) { 
  if(model == null || !(model.exists))
    throw MError("Document models may not be null.")

  /* The used GrGEN.NET instance */
  //private val grgen = new GrGEN(MODEL_HOME, model.name)
  
  /* Parse locutor document model <code>model</code> into GrGEN.NET graph rewrite system.  That is a graph model (.gm), 
   * a set of graph rewrite rules (.grg), and a graph creatin script (.grs) */
//  val (gm, gr, gs) = parse
//  assert(grgen.model(gm).exists)
//  assert(((p: (File, File)) => p._1.exists && p._2.exists)(grgen.rules(gr)))
//  assert(grgen.script(gs).exists)
  
  // ///////////// draft
//  def ripple = {
//    // gm, gr only have to be generated once; gs however has to be adapted according to the current delta to ripple
//    val p = scaup.io.Process("/opt/grgen/bin/GrShell -N "+(MODEL_HOME/model.name/(model.name+".grs")).ap, null, MODEL_HOME/model.name, false)
//    p.waitFor match {
//      case 0 => println("bingo")
//      case _ => println(p.stderr.mkString)
//    }    
//  }
//  ripple
  // ///////////// draft
  
//  /**
//   * @param  model the document model to parse 
//   * @return (GrGEN.NET#GraphModel, GrGEN.NET#GraphRules, GrGEN.NET#GraphScript) */
//  private def parse: (List[File], File, File) = {
//    
//    phrase(dm)(new lexical.Scanner(model.content)) match {
//      case Success(result, _) => result
//      case n@_                => throw new MError(format("M parsing error in ''{0}'': " + n, model))
//    }
//    
//    (Fake.graphModels, Fake.graphRules, Fake.graphScript)
//  } 
}

private [model] object Fake {
  
  val graphModels = "src/test/locutor/spec/model/docbook.gm".toFile :: Nil
  
  val graphRules = "using svnbook;" >>> "src/test/locutor/spec/model/svnbook.grg".toFile
  
  def graphScript = """
new b:Book       (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/book.xml", xpath="/")
new f:Preface    (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/foreword.xml", xpath="/")
new ch00:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch00-preface.xml", xpath="/")
new ch01:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch01-fundamental-concepts.xml", xpath="/")
new ch02:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch02-basic-usage.xml", xpath="/")
new ch03:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch03-advanced-topics.xml", xpath="/")
new ch04:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch04-branching-and-merging.xml", xpath="/")
new ch05:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch05-repository-admin.xml", xpath="/")
new ch06:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch06-server-configuration.xml", xpath="/")
new ch07:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch07-customizing-svn.xml", xpath="/")
new ch08:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch08-embedding-svn.xml", xpath="/")
new ch09:Chapter (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/ch09-reference.xml", xpath="/")
new aa:Appendix  (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/appa-quickstart.xml", xpath="/")
new ab:Appendix  (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/appb-svn-for-cvs-users.xml", xpath="/")
new ac:Appendix  (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/abbc-webdav.xml", xpath="/")
new cp:Appendix  (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/copyright.xml", xpath="/")
new i:Index      (url="file:///Users/nmueller/locutor/src/locutor/etc/grgen/svnbook/index.xml", xpath="/")

new f     -:requiredBy(via=File)-> b
new ch00  -:requiredBy(via=File)-> b
new ch01  -:requiredBy(via=File)-> b
new ch02  -:requiredBy(via=File)-> b
new ch03  -:requiredBy(via=File)-> b
new ch04  -:requiredBy(via=File)-> b
new ch05  -:requiredBy(via=File)-> b
new ch06  -:requiredBy(via=File)-> b
new ch07  -:requiredBy(via=File)-> b
new ch08  -:requiredBy(via=File)-> b
new ch09  -:requiredBy(via=File)-> b
new aa    -:requiredBy(via=File)-> b
new ab    -:requiredBy(via=File)-> b
new ac    -:requiredBy(via=File)-> b
new cp    -:requiredBy(via=File)-> b
new i     -:requiredBy(via=File)-> b
""" >>> "src/test/locutor/spec/model/svnbook.grs".toFile
  
}
